<?php
class classSpent {

	/**
	* Inserts a new spent
	*
	* @param Int		Category ID
	* @param String	Date (dd/mm/yyyy)
	* @param Float		Spent's value (two decimals: eg.:5.35)
	* @param String	Description
	*/
	public function insertSpent($intCategory, $strDate, $flValue, $intPayments = 1,$strDescription) {

		if(!$this->CheckCategoryById($intCategory)) {
			echo "<h3>The category doesn't exists.</h3>";
			return false;
		}

		$arrDate = explode("/", $strDate);
		$strDate = implode("-", Array($arrDate[2], $arrDate[1], $arrDate[0]));

		$bDB = new classDB(BHOST, BUSER, BPSW, BDB);
		$bDB->insertSpent($intCategory, $strDate, $flValue, (isset($intPayments) ? $intPayments : 1),$strDescription);
      unset($bDB);
		return;
	}

	/**
	* returns all the categories or false if none
	*
	* @return Dataset|Bool
	*/
	public function getCategories() {

		$strQuery = "
			SELECT
				id,
				name,
				description
			FROM
				category
			ORDER BY
				name"
		;
		$bDB = new classDB(BHOST, BUSER, BPSW, BDB);
		$arrCategories = $bDB->query($strQuery, MYSQL_OBJECT);

		if(count($arrCategories) > 0)
			return $arrCategories;
		else
			return false;
	}

	public function getSpentHistory($strStartDate, $strEndDate) {

		if(!$this->checkDateRange($strStartDate, $strEndDate))
			return false;

		$strQuery = "
			SELECT
				id,
				category_id,
				category_name
				date
				value,
				description
			FROM
				spent
			WHERE date BETWEEN '$strStartDate' AND '$strEndDate'"
		;
      $bDB = new classDB(BHOST, BUSER, BPSW, BDB);
		$arrCategories = $bDB->query($strQuery, MYSQL_OBJECT);

		if(count($arrCategories) > 0)
			return $arrCategories;
		else
			return false;
	}

	/**
	* Checks if the given category ID exists
	*
	* @param Int Category ID
	*/
	private function CheckCategoryById($intCategory) {

		$bDB = new classDB(BHOST, BUSER, BPSW, BDB);

		if(!$bDB->getCategoryById($intCategory))
			return false;
		else
			return true;

		unset($bDB);
	}

	/**
	* Checks if the given category name ecxists
	*
	* @param String Category name
	*/
	private function CheckCategoryByName($strCategory) {

   	$bDB = new classDB(BHOST, BUSER, BPSW, BDB);

		if(!$bDB->getCategoryByName(strtolower($strCategory)))
			return false;
		else
			return true;

		unset($bDB);
	}

	/**
	* Checks if the given date range in a valid one
	*
	* @param String Start date
	* @param String End date
	*
	* @return Bool
	*/
	private function checkDateRange($strStartDate, $strEndDate) {

		$arrStartDate	= explode("/", $strStartDate);
		$arrEndDate		= explode("/", $strEndDate);

		if(!checkdate($arrStartDate[1], $arrStartDate[0], $arrStartDate[2])) {
			echo "Wrong date value for strStartDate: $strStartDate";
			return false;
		}

   	if(isset($strEndDate)) {
         if(!checkdate($arrStartDate[1], $arrStartDate[0], $arrStartDate[2])) {
				echo "Wrong date value for strEndtDate: $strEndDate";
				return false;
			}
		}

		$intStartDate	= mktime(0, 0, 0,	$arrStartDate[1],	$arrStartDate[0],	$arrStartDate[2]);
      $intEndDate		= mktime(0, 0, 0,	$arrEndDate[1],	$arrEndDate[0],	$arrEndDate[2]);
      if($intEndDate <= $intStartDate) {
			echo "This is not a valid date range: $strStartDate - $strEndDate";
			return false;
      }
      else
      	return true;
	}
}
?>
